#!/bin/bash
# installation nginx
# nginx.org

[ "$EUID" -ne 0 ]  && {
        echo -e "[\e[36m$(date +%T)\e[0m] [\e[32mINFO\e[0m] please run this script with root"
        exit 1
}

exec 3>&1

#加载自定义函数库
FUNCTIONS_NAME="1_pub_funtions_makai"
for i in $FUNCTIONS_NAME ; do
	#获取库文件路径
	FUNCTIONS_URL="$(find / -path /proc -prune -o -name ${i} -print)"
	#获取不到库文件则报错并退出
	${FUNCTIONS_URL:? Not Found}
	#获取到则加载库文件
	. $FUNCTIONS_URL
done

# DEBUG=0 关闭调试
# DEBUG=1 开启调试 (默认)
#       STEP_THROUGH=1 遇到错误时暂停 (默认)
#       STEP_THROUGH=2 不管是否有错误都暂停
# DEBUG=2 遇到错误就退出
DEBUG="1"
STEP_THROUGH="1"
#限制变量取值范围
if [ $# -gt 0 ] ; then
	case "$1" in
		0|1|2)
			DEBUG="$1" ;;
	esac
	case "$2" in
		1|2)
			STEP_THROUGH="$2" ;;
	esac
fi
 
alert() {
        local RET_CODE=$?
        if [ -z "$DEBUG" ] || [ "$DEBUG" -eq 0 ] ; then
                return
        fi

        if [ "$RET_CODE" -ne 0 ] ; then
                echo " Warning: $* " >&3
                [ "$DEBUG" -eq 2 ] && exit "$RET_CODE"
                [ "$STEP_THROUGH" = 1 ] && {
                        echo "Press [Enter] to containue" >&3; read x
                }
        fi

        [ "$STEP_THROUGH" = 2 ] && {
                echo "Press [Enter] to continue" >&3; read x
        }
}


##源码安装nginx
	# nginx版本
	DEFAULT_NGINX_VERSION="nginx-1.20.2"
	NGINX_VERSION=${NGINX_VERSION:-$DEFAULT_NGINX_VERSION}

	# 安装路径
        DEFAULT_INSTALL_URL="/usr/local"
        INSTALL_URL=${INSTALL_URL:-$DEFAULT_INSTALL_URL}

	# nginx模块
	DEFAULT_NGINX_MODULE="--user=nginx --prefix=${INSTALL_URL}/nginx --with-http_ssl_module"
	NGINX_MODULE=${NGINX_MODULE:-$DEFAULT_NGINX_MODULE}

        [ ! -d ${INSTALL_URL}/nginx ]
        alert "${INSTALL_URL}/nginx already exists"

	while [ $# -gt 0 ] ; do
		case "$1" in
			--version)
				NGINX_VERSION="nginx-$2"
				shift
				;;
			--*)
				echo "Illegal option $1"
				;;
		esac
		shift $(( $# > 0 ? 1 : 0 ))
	done

	logW "This is ${ostype}"
	logW "start install ${NGINX_VERSION}"
exit
body() {
	#install software
	which yum && {
		yum clean all
		for i in gcc make tar pcre-devel openssl-devel zlib-devel ; do
			yum install -y $i
			alert "$i install failed" 
		done
	}

	which apt && {
		sudo apt-get clean
		sudo apt-get update
		for i in gcc make tar build-essential libpcre3 libpcre3-dev zlib1g-dev openssl libssl-dev ; do
			sudo apt-get install -y $i
			alert "$i install failed" 
		done
	}
	
	#download
	[ ! -f /tmp/${NGINX_VERSION}.tar.gz ] && {
		echo "download..."
		curl -# -k -o /tmp/${NGINX_VERSION}.tar.gz https://nginx.org/download/${NGINX_VERSION}.tar.gz
		alert 'download failed'
	} >&3

	#tar file
	[ ! -d /tmp/${NGINX_VERSION} ] && {
		tar -xzf /tmp/${NGINX_VERSION}.tar.gz -C /tmp
		alert 'tar failed'
	}

	#configure and install
	[ -d /tmp/${NGINX_VERSION} ] && {
		cd /tmp/$NGINX_VERSION && ./configure $NGINX_MODULE
		alert 'configure failed'
		make
		alert 'make failed'
		make install >&3
		alert 'make install failed'
	}

	#add user
	! id nginx && {
		useradd -M -s /sbin/nologin nginx
		if [[ ! $? -eq 0 && ! $? -eq 9 ]] ; then
			alert 'add user failed'
		fi
	}

	#start nginx service
	[ $(id -u) -eq 0 ]  && {
		cat >/usr/lib/systemd/system/nginx.service <<-EOF
		[Unit]
		Description=The Nginx HTTP Server
		After=network.target remote-fs.target nss-lookup.target

		[Service]
		Type=forking
		PIDFile=/run/nginx.pid
		ExecStartPre=/usr/bin/rm -f /run/nginx.pid
		ExecStartPre=${INSTALL_URL}/nginx/sbin/nginx -t -q
		ExecStart=${INSTALL_URL}/nginx/sbin/nginx -g "pid /run/nginx.pid;"
		ExecReload=${INSTALL_URL}/nginx/sbin/nginx -t -q
		ExecReload=${INSTALL_URL}/nginx/sbin/nginx -s reload -g "pid /run/nginx.pid;"

		ExecStop=/bin/kill -s HUP \${MAINPID}
		KillSignal=SIGQUIT
		TimeoutStopSec=5
		KillMode=process
		PrivateTmp=ture

		[Install]
		WantedBy=multi-user.target
		EOF
		systemctl daemon-reload
		systemctl enable --now nginx
		alert 'service start failed'

	}

	#放行端口
	[ $ostype = 'centos.7' ] && {
		get_Firewalld_status #23#
		if [ $? -eq 0 ]; then
			firewall-cmd --zone=public --add-port="80"/"tcp" --permanent
			firewall-cmd --reload
		fi
	}

	echo && {
		"${INSTALL_URL}/nginx/sbin/nginx" -V
		echo "-----------------------------------------------------------------------------------"
		ss -tupln | grep -E "Netid|nginx"
	} >&3

} &>/dev/null

body

## 下载并配置wordpress
wordpress_Download() {

#------------------------------wordpress--------------------------------------------------
logW "1. 开始下载wordpress 到/usr/local/nginx/html"
logY "curl -# --output /tmp/latest-zh_CN.zip -O https://cn.wordpress.org/latest-zh_CN.zip"
if [[ -d /usr/local/nginx/html ]]; then
  curl -# --output /tmp/latest-zh_CN.zip -O https://cn.wordpress.org/latest-zh_CN.zip
  checkTF
else
  logR "路径不存在"
  checkTF
fi

logW "2. 开始解压wordpress"
logY "unzip /tmp/latest-zh_CN.zip -C /usr/local/nginx/html"
  yum install unzip -y
  unzip /tmp/latest-zh_CN.zip -d /usr/local/nginx/html
  checkTF

}

## nginx配置参考
nginx_Config() {

    server \{
        listen       80 default;
        server_name  www.njmgg.com njmgg.com;
        expires      12h;

        location / \{
            root   /usr/share/nginx/html/njmgg_html;
            index  index.html;
        \}

        rewrite ^/tmooc/$ /tmooc/index.html;
        rewrite ^/document$ /note/document.txt;
        rewrite ^/samba$ /note/samba.pdf;
        rewrite ^/nginx$ /note/nginx.pdf;
        rewrite ^/ansible$ /note/ansible.pdf;
        rewrite ^/facts$ /note/facts.pdf;
        rewrite ^/rhce$ /note/RHCE_MAKAI.pdf;
    \}


    server \{
        listen       80;
        server_name  www.zhoujiao.top zhoujiao.top;
        expires      12h;

        location / \{
            root   /usr/share/nginx/html/zj_html;
            index  index.html;
        \}
    \}

}
